package com.jintu.portal.service.datadriver.receiver;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.service.datadriver.repository.SourceDataMongoDao;
import com.jintu.portal.cache.RedisHelper;
import com.jintu.portal.mq.RabbitMqSender;
import com.jintu.portal.pojo.RetEntity;
import com.jintu.portal.pojo.mq.NotificationEntity;
import com.jintu.portal.pojo.submit.DataResponse;
import com.jintu.portal.service.datadriver.parser.ParserCommon;
import com.jintu.portal.service.datadriver.pojo.MsgRespCode;
import com.jintu.portal.service.datadriver.pojo.RespInfo;
import com.jintu.portal.service.datadriver.pojo.Response;
import com.jintu.portal.service.datadriver.pojo.SysRespCode;
import com.jintu.portal.service.datadriver.pojo.entity.SourceData;
import com.jintu.portal.utils.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.bson.Document;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @author very
 */
@Slf4j
@RestController
@RequestMapping("/receive/zmScoreReceiver")
public class ZmScoreReceiver {

    @Resource(name = "sourceDataMongoDao")
    private SourceDataMongoDao sourceDataMongoDao;

    @Resource(name = "redisHelper")
    private RedisHelper redisHelper;

    @Resource(name = "rabbitMqSender")
    private RabbitMqSender rabbitMqSender;

    @RequestMapping(method = RequestMethod.POST)
    public JSONObject receive(@RequestBody Response response){
        JSONObject ret = new JSONObject();
        ret.put("code", "0000");
        JSONObject notification = JSONObject.parseObject(response.getMerchPrivate());
        if(SysRespCode.QUERY_SUCCESS.getCode().equals(response.getRetCode())){
            RespInfo respInfo = response.getRetInfo();
            if(MsgRespCode.SEARCH.getCode().equals(respInfo.getCode())){
                JSONObject msgJson = (JSONObject) respInfo.getMessage();
                Integer score = msgJson.getInteger("sesameScore");
                log.error("风控订单[{}] - 数据请求订单为[{}] 接收到了[name: {}]回调，成功获取分数 [{}]",
                        notification.getString("orderId"), response.getOrderId(), msgJson.getString("nameOnApp"), score);
                Document savingData = new Document();
                savingData.put("score", score);
                DataResponse dataResponse = DataResponse.build(RetEntity.SUCCESS, 1);
                dataResponse.setOrderId(notification.getString("orderId"));
                SourceData sourceData = ParserCommon.wrapSourceSavingData(
                        dataResponse, notification.getInteger("userId"), savingData);
                try {
                    sourceDataMongoDao.saveSourceData(sourceData, notification.getString("dataSourceCode"));
                } catch (Exception e) {
                    log.error("存储芝麻分报错");
                    log.error(e.getMessage(), e);
                }
                redisHelper.hset(
                        CommonUtil.getDataSourceRedisKey(notification.getString("orderId"), notification.getInteger("userId")),
                        notification.getString("dataSourceCode"), "1");
                try {
                    NotificationEntity sendingData = NotificationEntity.build(
                            notification.getInteger("userId"),
                            notification.getString("username"),
                            notification.getString("orderId"),
                            notification.getString("dataSourceCode"));
                    rabbitMqSender.sendMessage(sendingData);
                    dataResponse.setRetCode(RetEntity.SUCCESS.getCode());
                    dataResponse.setRetMessage(RetEntity.SUCCESS.getMessage());
                } catch (Exception e) {
                    log.error("芝麻分发送清洗MQ报错");
                    log.error(e.getMessage(), e);
                    dataResponse.setRetCode(RetEntity.INNER_ERROR.getCode());
                    dataResponse.setRetMessage(RetEntity.INNER_ERROR.getMessage());
                }
            } else {
                log.warn("风控订单[{}] - 芝麻分未获取分数", notification.getString("orderId"));
            }
        } else {
            log.warn("风控订单[{}] - 芝麻分未获取分数", notification.getString("orderId"));
        }
        return ret;
    }
}
